package parser;

import game.MoveExecutionList;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

import chess.Board;
import chess.Player;

public abstract class ChessMoveParser implements ParserErrorConstants {
	public static final String DEFAULT_SAVE_LOCATION = "Saved Files/";
	
	protected JFileChooser fileChooser;
	protected File file;
	protected File outputFile;
	
	protected int errorCode;
	protected Board board;
	protected MoveExecutionList listOfMoves;
	protected ArrayList<Player> players;
	protected Player currentPlayer;
	
	protected String chessExtension;
	
	public ChessMoveParser(Board board, ArrayList<Player>players) {
		this.board = board;
		this.players = players;
		currentPlayer = players.get(0);
		listOfMoves = new MoveExecutionList();
		setExtension();
	}

	/**
	 * This method will set up the file to be exported with all the move list data. It will
	 * pop up a window for the user to select the file with the extension .cg
	 * @param frame The main frame for the file chooser
	 */
	public void setUpLoadChooser(JFrame frame) {
		setUpFileChooser(frame);
		if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
			file = fileChooser.getSelectedFile();
		}
	}
	/**
	 * This method will setup the file chooser. It take a frame as a parameter. This file chooser
	 * allows for a file with the extension of  .cg to be selected. This method will initialize
	 * the file chooser at the default location of SavedF Files
	 * @param frame The main frame that the file chooser is the child of
	 */
	public void setUpFileChooser(JFrame frame){
		fileChooser = new JFileChooser();
		fileChooser.addChoosableFileFilter(new FileFilter(){
			public boolean accept(File f){
				if(f.isDirectory()){
					return true;
				}
				String fileURL = f.getName();
				String extension = fileURL.substring(fileURL.lastIndexOf('.') + 1);
				extension = extension.toLowerCase();
				if(extension.equals(chessExtension)){
					return true;
				}
				return false;
			}
			public String getDescription(){
				return "Chess Move List (." + chessExtension + ")";
			}
		});
		fileChooser.setCurrentDirectory(new File(DEFAULT_SAVE_LOCATION));
	}
	/**
	 * This method will get an error code if the writeFile() method returns a false or 
	 * if the parseFile() method returns a null value
	 * @return Returns the code on which error has occurred
	 */
	public int getErrorCode() {
		return errorCode;
	}

	/**
	 * This method will set up the file that the move list is to be saved to. It will pop up a 
	 * window for the user to save the file with the extension .cg
	 * @param frame The main frame for the file chooser
	 */
	public void setUpSaveChooser(JFrame frame) {
		setUpFileChooser(frame);
		if(fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION){
			String fileName = fileChooser.getSelectedFile().toString();
			if(fileName.contains(".")){
				fileName = fileName.substring(0, fileName.lastIndexOf("."));
			}
			File newFile = new File(fileName + "." + chessExtension);
			outputFile = newFile;
		}
		
	}

	/**
	 * This will get the index of the current player
	 * @return The index of the current player
	 */
	public int getCurrentPlayer() {
		return players.indexOf(currentPlayer);
	}
	public boolean isAbleToParse(){
		return file != null; 
	}
	/**
	 * The abstract method when called will write to a file
	 * @return Returns a true if the write was successful 
	 */
	public abstract boolean writeFile();
	/**
	 * This method will parse a file
	 * @return Returns a Move Execution List of the parsing was successful
	 */
	public abstract MoveExecutionList parseFile();
	/**
	 * This method will set the extension of the files to be selected
	 */
	public abstract void setExtension();
}